extern "C"
{
    #include "timeaction.h"
};

#include "globaldata.h"

#include <Windows.h>
#include <imagehlp.h>
#pragma comment(lib,"imagehlp.lib")
#include <stdio.h>

void outimportfunc()
{
    char *dll_name[1024];
    char* function_name[1024];
    HMODULE hInstance; 
    int outloop_index=0;

    PIMAGE_THUNK_DATA thunk;
    hInstance=GetModuleHandle("SDL.dll");
    IMAGE_DOS_HEADER *dosheader= (IMAGE_DOS_HEADER *)hInstance;
    IMAGE_NT_HEADERS *ntheader= (IMAGE_NT_HEADERS *)((DWORD)hInstance + dosheader->e_lfanew);
    IMAGE_DATA_DIRECTORY *pSymbolTable= &ntheader->OptionalHeader.DataDirectory[1];
    IMAGE_IMPORT_DESCRIPTOR *pImportDesc =(IMAGE_IMPORT_DESCRIPTOR *)((DWORD)hInstance + pSymbolTable->VirtualAddress);

    printf("the module is load at:%d\n",hInstance);
    printf("the pointer to ImportDesc is: %d\n",pImportDesc);

    while(pImportDesc ->FirstThunk)
    {
        dll_name[outloop_index]=(char*)((PBYTE)hInstance+pImportDesc->Name);
        printf("------%s-------\n",dll_name[outloop_index]);
        outloop_index++;

        thunk=( PIMAGE_THUNK_DATA)((PBYTE)hInstance+ pImportDesc->OriginalFirstThunk);

        int x=0;
        while(thunk->u1.Function)
        {
            function_name[x]=(char*)((PBYTE)hInstance +
                ( DWORD)thunk->u1.AddressOfData+2);
            printf("%s\n",function_name[x]);

            x++; 
            thunk++;
        }

        pImportDesc++;
    }
}

int main(int argc, char **argv)
{
    outimportfunc();
    globaldata::createTimeActionMgr(1024);
    globaldata::createGameStage();
    
    globaldata::exportGui();
    globaldata::doluafile("handler.lua");
    globaldata::doluafile("test.lua");
    globaldata::doluafile("main.lua");
    globaldata::createClientSocket();

    while(true)
    {
        globaldata::showGameStage();
        timeaction_mgr_schedule(globaldata::getTimeActionMgr());
        //globaldata::runClientSocket();
    }

    return 0;
}